#!/bin/bash

# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export PATH=`pwd`/cicd/h5:$PATH

source check_env.sh
OHOS_BASE_SDK_HOME=
OHPM_FILE_DIR=$ROOT_DIR/ohpm/bin
HAP_FILE_DIR=$ROOT_DIR/out/tpc
HOS_SDK_HOME=
function check_prepare() {
    if [ ! -d $OHOS_SDK_HOME ]
    then
        echo "SDK not prepare! start prepare.sh first!"
        return 1
    fi
    export OHOS_BASE_SDK_HOME="$OHOS_SDK_HOME"
    export HOS_SDK_HOME="$OHOS_SDK_HOME"
    if [ ! -d $NODE_HOME ]
    then
        echo "Nodejs not prepare! start prepare.sh first!"
        return 1
    fi
    if [ ! -d $OHPM_FILE_DIR ]
    then
        echo "ohpm not prepare! start prepare.sh first!"
        return 1
    fi
    export NODE_HOME=${NODE_HOME}
    export PATH=$NODE_HOME/bin:$PATH
    export PATH=$PATH:$OHOS_SDK_HOME:$OHPM_FILE_DIR:$NODE_HOME:$OHOS_BASE_SDK_HOME

    return 0
}

function check_out_dir() {
    if [ ! -d $HAP_FILE_DIR ]
    then
        mkdir -p $HAP_FILE_DIR
    else
        rm -rf $HAP_FILE_DIR/*
    fi
}

function ohpm_init() {
    echo "ohpm init!"
    $NODE_HOME/bin/npm config set @ohos:registry=https://repo.harmonyos.com/npm/
    $NODE_HOME/bin/npm config set lockfile=false

    cd $OHPM_FILE_DIR
    bash init
    cd $OLDPWD
}

function build_init() {
    echo "build_init ########"
    if [ -z "$COMPONT_NAME" ]
    then
        echo "no compont to build!"
        return 1
    fi

    if [ -e "$COMPONT_NAME/.gitmodules" ]
    then
        echo "***********check gitmodules************"
        for submodule_path in `cat $COMPONT_NAME/.gitmodules | grep "path ="`
        do
            if [[ $submodule_path == "path" ]] || [[ $submodule_path == "=" ]]
            then
                continue
            fi
            if [ ! -d "$COMPONT_NAME/$submodule_path" ]
            then
                echo "get submodules path: $COMPONT_NAME/$submodule_path failed!"
                return 1
            fi
            cd $COMPONT_NAME/$submodule_path
            git submodule update --init --recursive
            cd $OLDPWD
        done
    fi

    return 0
}

function check_prebuild()
{
    if [ ! -e "./prebuild.sh" ]
    then
        return 0
    fi

    bash ./prebuild.sh
    if [ $? -ne 0 ]
    then
        echo "prebuild failed!!!"
        return 1
    fi

    return 0
}

function prepare_commandlinetools(){
    echo "prepare_commandlinetools"

    if [ -d "command-line-tools" ];then
        echo "command-line-tools exists, do not download again."
    else
        if [ ! -f "command-line-tools.zip" ];then
            echo "start download command-line-tools.zip"
            curl -o "commandline-tools-linux.zip" https://repo.huaweicloud.com/harmonyos/ohpm/5.0.5/commandline-tools-linux-x64-5.0.5.310.zip


            # 判断是否下载成功
            if [ $? != 0 ];then
                echo "commandline-tools-linux.zip failed."
                rm -rf commandline-tools-linux.zip
                return 1
            fi
        fi

        # 开始解压zip包
        echo "start unzip command-line-tools.zip"
        unzip -oqq "./command-line-tools.zip"

        echo "finish unzip commandline-tools-linux.zip"
    fi

    return 0
}

function start_build() {
    echo "CI start compile**********************************"
    echo "OHOS_BASE_SDK_HOME=$OHOS_BASE_SDK_HOME"
    echo "OHOS_SDK_HOME=$OHOS_SDK_HOME"
    echo "SDK_API10_SHASUM=$SDK_API10_SHASUM"
    COMMAND_TOOLS_URL="https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_package_901_9/c3/v3/TDeTmwjNSumfFW9F6s5B4g/commandline-tools-linux-x64-5.0.7.210.zip?HW-CC-KV=V1&HW-CC-Date=20250328T013724Z&HW-CC-Expire=7200&HW-CC-Sign=4449C4F1F14057FE0529D02C6FB29598037408757E126FF244BB53BFEC59EAF4"
    ZIP_NAME=commandline-tools-linux.zip
    DIR_NAME=command-line-tools
    NODE_URL=https://mirrors.huaweicloud.com/nodejs/v18.20.7/node-v18.20.7-linux-x64.tar.gz
    NODE_DIR=node-v18.20.7-linux-x64



    local res=0
    if [ -z $COMPONT_NAME ]
    then
        echo "have not get the compont!"
        #return 1
	return 0
    fi

    if [ $COMPONT_NAME == "openharmony_tpc_samples" ]
    then
        echo "not need to compile openharmony_tpc_samples!!!"
        return 0
    fi
    CURWORK_DIR=$ROOT_DIR/$COMPONT_NAME
    cd $CURWORK_DIR
    
    if [ ! -f "build-profile.json5" ]
    then
        CURWORK_DIR=$ROOT_DIR/$COMPONT_NAME/h5_sample/h5_sample
        cd $CURWORK_DIR
        echo "当前目录没有build-profile.json5文件，已切换到 ./h5_sample/h5_sampl 目录"
    else
        echo "当前目录存在build-profile.json5文件，不需要切换目录"
    fi

    check_prebuild
    if [ $? -ne 0 ]
    then
        echo "check_prebuild failed!"
        #return 1
	return 0
    fi
    
    if [ -e "oh-package.json5" ]
    then
        for path in $(find . -name "oh-package.json5")
        do
            path_array=(${path//\// })
            lenth=${#path_array[@]}
            if [ $lenth -eq 3 ]
            then
                cd ${path_array[1]}
                ohpm install
                cd $OLDPWD
            fi
        done
        ohpm install

        echo "======wget1 COMMAND_TOOLS_URL -O commandline-tools-linux.zip"
        wget $COMMAND_TOOLS_URL -O commandline-tools-linux.zip

        echo "======unzip1 ZIP_NAME"
        unzip $ZIP_NAME

        echo "======ls"
        ls

        echo "======HVIGOR_HOME=$CURWORK_DIR/$DIR_NAME/hvigor"
        HVIGOR_HOME=$CURWORK_DIR/$DIR_NAME/hvigor

        echo "======export PATH=HVIGOR_HOME/bin:PATHr"
        export PATH=$HVIGOR_HOME/bin:$PATH

        echo "====== wget NODE_URL -O node.tar.gz"
        wget $NODE_URL -O node.tar.gz

        echo "======tar -xzf node.tar.gz"
        tar -xzf node.tar.gz

        echo "======ls"
        ls

        echo $NODE_HOME

        echo "unset NODE_HOME"
        unset NODE_HOME

        echo "======NODE_HOME=$CURWORK_DIR/$NODE_DIR"
        export NODE_HOME=$CURWORK_DIR/$NODE_DIR

        echo "export PATH=$NODE_HOME/bin:$PATH"
        export PATH=$NODE_HOME/bin:$PATH

        echo "======printenv"
        printenv

        echo "node -v"
        node -v

        echo "======npm config set "@ohos:registry" https://repo.harmonyos.com/npm/"
        npm config set "@ohos:registry" https://repo.harmonyos.com/npm/

        echo "======npm config set registry https://repo.huaweicloud.com/repository/npm/"
        npm config set registry https://repo.huaweicloud.com/repository/npm/
        # yum install -y mesa-libGL

        echo "apt install -y libgl1-mesa-dev"
        apt install -y libgl1-mesa-dev

        echo "apt update"
        apt update

        echo "apt install libc6"
        apt install libc6



        echo "hvigorw -v"
        hvigorw -v

        # bash hvigorw --mode module assembleHap assembleHar assembleHsp --no-daemon

        echo "hvigorw assembleHap --mode module -p product=default -p buildMode=debug --no-daemon"
    
        hvigorw --mode module assembleHap assembleHar assembleHsp --no-daemon
        hvigorw assembleHap --mode module -p product=default -p buildMode=debug --no-daemon

        echo "======hvigor.js"
        export PATH=$CURWORK_DIR/$DIR_NAME/hvigor/hvigor/bin:$PATH


        res=$?
    elif [ -e "package.json" ]
    then
        for path in $(find . -name "package.json")
        do
            path_array=(${path//\// })
            lenth=${#path_array[@]}
            if [ $lenth -eq 3 ]
            then
                cd ${path_array[1]}
                $NODE_HOME/bin/npm install
                cd $OLDPWD
            fi
        done
        $NODE_HOME/bin/npm install
        echo "NODE_HOME:$NODE_HOME"
        $NODE_HOME/bin/node $CURWORK_DIR/$DIR_NAME/hvigor/hvigor/bin/hvigor.js --mode module assembleHap
        res=$?
    else
        echo "can not support this project! miss package.json or oh-package.json5"
        res=1;
    fi

    if [ $res -eq 0 ]
    then
        for path in $(find . -name build-profile.json5 | grep -v "\./build-profile.json5" | grep -v "ohpm/" | grep -v "/build/")
        do
            path=${path:2}
            filepath=(${path//\// })
            cp -arf $CURWORK_DIR/$filepath/build/default/outputs $HAP_FILE_DIR
            if [ $? -ne 0 ]
            then
                res=1
                break
            fi
        done
    fi

    if [ $res -eq 0 ]
    then
        echo "************compile success****************"
    else
         echo "************compile fail****************"
    fi
    
    cd $ROOT_DIR

    #return $res
    #return 1
    return 0
}

function compile() {
    get_apiversion $1
    check_prepare
    check_out_dir
    ohpm_init
    build_init
    start_build
    return 0
}

compile $*
exit $?

#eof


    echo $NODE_HOME

    echo "unset NODE_HOME"
    unset NODE_HOME

    echo "======NODE_HOME=ROOT_DIR/NODE_DIR"
    export NODE_HOME=$ROOT_DIR/$NODE_DIR

    echo "export PATH=$NODE_HOME/bin:$PATH"
    export PATH=$NODE_HOME/bin:$PATH
